package com.ruoyi.zhjd.exp.modules.asphaltrequires.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ValidatorUtils;
import com.ruoyi.zhjd.exp.modules.asphaltrequires.dto.AsphaltRequiresDto;
import com.ruoyi.zhjd.exp.modules.asphaltrequires.entity.AsphaltRequiresEntity;
import com.ruoyi.zhjd.exp.modules.asphaltrequires.entity.AsphaltRequiresRefEntity;
import com.ruoyi.zhjd.exp.modules.asphaltrequires.mapper.AsphaltRequiresMapper;
import com.ruoyi.zhjd.exp.modules.asphaltrequires.mapper.AsphaltRequiresRefMapper;
import com.ruoyi.zhjd.exp.modules.asphaltrequires.service.AsphaltRequiresRefService;
import com.ruoyi.zhjd.exp.modules.asphaltrequires.service.AsphaltRequiresService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName: AsphaltRequiresController
 * @Description: 沥青类技术要求
 * @author zhaij
 * @since 2021-04-25
 */
@Service
public class AsphaltRequiresServiceImpl extends ServiceImpl<AsphaltRequiresMapper, AsphaltRequiresEntity> implements AsphaltRequiresService {
//    @Autowired
//    private BaseServiceComponent baseServiceComponent;

    @Autowired
    private AsphaltRequiresMapper requiresMapper;

    @Autowired
    private AsphaltRequiresRefMapper requiresRefMapper;

    @Autowired
    private AsphaltRequiresRefService requiresRefService;

    @Override
    public IPage<AsphaltRequiresDto> page(Map<String, Object> params) {
        IPage<AsphaltRequiresDto> page = null;
        return requiresMapper.selectPage(page,params);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveRequires(AsphaltRequiresDto dto) {
        QueryWrapper<AsphaltRequiresEntity> query = new QueryWrapper<>();
        query.eq("deleted","0").eq("name",dto.getName());
        query.ne(StringUtils.isNotBlank(dto.getId()),"id",dto.getId());
        long result = requiresMapper.selectCount(query);
        if(result > 0){
            throw new ServiceException("名称"+dto.getName()+"的技术要求已经存在，请重新输入！");
        }
        AsphaltRequiresEntity entity = new AsphaltRequiresEntity();
        BeanUtils.copyProperties(dto,entity);
        if(StringUtils.isNotBlank(entity.getId())){
            requiresMapper.updateById(entity);
        }else{
            requiresMapper.insert(entity);
        }
        this.insertRequiresRef(entity.getId(),dto);
    }

    private void insertRequiresRef(String id, AsphaltRequiresDto dto) {
        QueryWrapper<AsphaltRequiresRefEntity> deleteWrap = new QueryWrapper<>();
        deleteWrap.eq("asphalt_id",id);
        requiresRefMapper.delete(deleteWrap);
        if(!CollectionUtils.isEmpty(dto.getRefs())){
            List<AsphaltRequiresRefEntity> list = dto.getRefs().stream().map(ref -> {
                ValidatorUtils.validate(ref);
                ref.setAsphaltId(id);
                AsphaltRequiresRefEntity re = new AsphaltRequiresRefEntity();
                BeanUtils.copyProperties(ref,re);
                return re;
            }).collect(Collectors.toList());
            requiresRefService.saveBatch(list);
        }
    }

    @Override
    public AsphaltRequiresDto detail(String id) {
        return requiresMapper.detail(id);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeRequires(String id) {
        QueryWrapper<AsphaltRequiresRefEntity> deleteWrap = new QueryWrapper<>();
        deleteWrap.eq("asphalt_id",id);
        requiresRefMapper.delete(deleteWrap);
        requiresMapper.deleteById(id);
    }
}
